<template>
  <a-modal
    :title="$t('新增')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :okText="$t('确定')"
    @cancel="handleCancel"
    :cancelText="$t('取消')"
    v-drag
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="dataForm" ref="form" :rules="rules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('商品')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsId">
              <a-select
                v-model="dataForm.goodsId"
                show-search
                :placeholder="$t('请输入商品编码/名称/条码')"
                :filter-option="false"
                :not-found-content="null"
                @search="handleGoodsSearch"
              >
                <a-select-option v-for="good in addGoodsList" :key="good.id" @click="changeGoods(good)">{{
                  good.name
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('商品售价')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input v-model="dataForm.price" :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('秒杀数量')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seckillNum">
              <a-input v-model="dataForm.seckillNum" :placeholder="$t('请输入秒杀数量')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('秒杀价')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seckillPrice">
              <a-input v-model="dataForm.seckillPrice" :placeholder="$t('请输入秒杀价')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('会员限购数量')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberLimit">
              <a-input v-model="dataForm.memberLimit" :placeholder="$t('请输入会员限购数量')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('秒杀说明')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="dataForm.descr" :placeholder="$t('请输入秒杀说明')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { postAction, httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { getAction, putAction } from '../../../api/manage'

export default {
  name: 'AddSeckillGoodsModal',
  data() {
    return {
      width: 800,
      visible: false,
      dataForm: {
        goodsId: '',
        price: '',
        seckillNum: '',
        seckillPrice: '',
        memberLimit: '1',
        descr: '',
      },
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      dataForm: {
        goodsId: '',
      },
      rules: {
        goodsId: [{ required: true, message: this.$t('请选择商品') }],
        price: [{ required: true ,message: this.$t('请选择商品售价')}],
        seckillNum: [{ required: true, message: this.$t('请输入商品秒杀数量') }],
        seckillPrice: [{ required: true, message: this.$t('请输入大于0的秒杀价'),pattern:/^([1-9]\d*(\.\d*[1-9][0-9])?)|(0\.\d*[1-9][0-9])|(0\.\d*[1-9])$/, }],
        memberLimit: [{ required: true, message: this.$t('请输入会员限购数量') }],
      },
      addGoodsList: [],
    }
  },
  methods: {
    add() {
      this.visible = true
      this.dataForm = {
        sort:1,
        price: '',
        seckillNum: '',
        seckillPrice: '',
        memberLimit: '1',
        descr: '',
      }
      this.getGoodsList()
    },
    handleGoodsSearch(value) {
      this.getGoodsList(value)
    },
    changeGoods(good) {
      console.log(good)
      this.dataForm.price = good.price
      this.dataForm.goodsName = good.name
    },
    getGoodsList(value) {
      let that = this
      let querydata = {}
      querydata.pageNo = 1
      querydata.pageSize = 9999
      querydata.goodsInfo = value
      querydata.form = '1,2,3,4,5,8'
      getAction('/goods/bisGoodsInfor/list', querydata).then((res) => {
        if (res.success) {
          that.addGoodsList = res.result.records
        }
      })
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('ok', this.dataForm)
          this.close()
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
  },
}
</script>
<style>
.batchForm .ant-form-item-label {
  width: 30%;
}
.batchForm .ant-form-item-control-wrapper {
  width: 62.5%;
}
</style>